/**
 * Project Name:mauto-web
 * File Name:TechnicianAction.java
 * Package Name:com.mingsoft.technician.action
 * Date:2017年6月14日上午9:38:11
 * Copyright (c) 2017, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.jy.web;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.jy.entity.JyEnterprise;
import com.inftech.mauto.jy.entity.Sex;
import com.inftech.mauto.jy.entity.Technician;
import com.inftech.mauto.jy.service.JyEnterpriseService;
import com.inftech.mauto.jy.service.TechnicianService;
import com.mingsoft.base.action.BaseAction;
import com.mingsoft.basic.constant.e.SessionConstEnum;
import com.mingsoft.basic.entity.ManagerSessionEntity;
import com.mingsoft.util.StringUtil;

import net.mingsoft.basic.util.BasicUtil;

/**
 * ClassName:TechnicianAction <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Date: 2017年6月14日 上午9:38:11 <br/>
 * 
 * @author Administrator
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/${managerPath}/technician")
public class TechnicianAction extends BaseAction{
	Log log = LogFactory.getLog(TechnicianAction.class);
	/**
	 * 技师Service
	 */
	@Resource(name = "technicianBizImpl")
	private TechnicianService technicianBizImpl;

	/**
	 * 企业Service
	 */
	@Resource(name = "jyEnterpriseBizImpl")
	private JyEnterpriseService JyEnterpriseBizImpl;
	
	/**
	 * 
	 * getTechnicianList:(技师列表). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/list.do")
	public String getTechnicianList(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		
		String name = request.getParameter("name") == null ? "" : request.getParameter("name").trim();
		String wxCarNumber = request.getParameter("wxCarNumber") == null ? ""
				: request.getParameter("wxCarNumber").trim();
		String enterpriseName = request.getParameter("enterpriseName") == null ? ""
				: request.getParameter("enterpriseName");
		map.put("name", name);
		map.put("wxCarNumber", wxCarNumber);
		map.put("enterpriseName", enterpriseName);
		BasicUtil.startPage();
		List<Technician> technicianList = technicianBizImpl.getTechnicianList(map);
		for(Technician technician:technicianList){
			if("0".equals(technician.getSex())){
				technician.setSex("男");
			}else if("1".equals(technician.getSex())){
				technician.setSex("女");
			}
		}
		BasicUtil.endPage(technicianList);
		List<Sex> sexList = new ArrayList<Sex>();
		Sex sex = new Sex();
		Sex sex1 = new Sex();
		sex.setSex("男");
		sex.setId("0");
		sex1.setSex("女");
		sex1.setId("1");
		sexList.add(sex);
		sexList.add(sex1);
		List<JyEnterprise> JyEnterpriseList= JyEnterpriseBizImpl.getJyEnterpriseList1();
		request.setAttribute("technicianList", technicianList);
		request.setAttribute("JyEnterpriseList", JyEnterpriseList);
		request.setAttribute("sexList", sexList);
		request.setAttribute("pageNo",
				CommonUtils.isBlank(request.getParameter("pageNo")) ? 1 : request.getParameter("pageNo"));
		return "/WEB-INF/manager/mauto/jy/technician";
	}

	/**
	 * 
	 * save:(新增技师). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = { "save.do" })
	public String save(HttpServletRequest request, HttpServletResponse response) {
		ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
				SessionConstEnum.MANAGER_SESSION);
		String name = request.getParameter("name").trim();
		String phone = request.getParameter("phone").trim();
		String companyId = request.getParameter("enterpriseName").trim();
		String sex = request.getParameter("sex").trim();
		String wxCarNumber = request.getParameter("wxCarNumber").trim();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("phone", phone);
		map.put("companyId", companyId);
		map.put("sex", sex);
		map.put("createBy", managerSession.getManagerId());
		map.put("pwd", StringUtil.Md5("123456"));
		map.put("wxCarNumber", wxCarNumber);
		int row = technicianBizImpl.getCountByTel(phone);
		if(row>0){
			map.put("result", "false");
			map.put("resultMsg", "已存在该用户！");
			JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
			outJson(response, jsonObject);
			return null;
		}
		try{
			technicianBizImpl.insertTechnician(map);
			map.put("result", "true");
		}catch(Exception e){
			log.error(e);
			map.put("resultMsg", "系统异常！");
		}
		JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
		outJson(response, jsonObject);
		return null;
	}
	
	/**
	 * 
	 * allDelete:(删除技师). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/allDelete.do")
	public String allDelete(HttpServletRequest request, HttpServletResponse response){
		Enumeration<String> enumeration = request.getParameterNames();
		String name = null;
		List<String> list = new ArrayList<String>();
		while(enumeration.hasMoreElements()){
			name = enumeration.nextElement();
			list.add(request.getParameter(name));
		}
		int row = technicianBizImpl.deleteAll(list);
		Map<String,Object> map = new HashMap<String,Object>();
		if(row>0){
			map.put("result", "true");
		}
		JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
		outJson(response, jsonObject);
		return null;
	}
	/**
	 * 
	 * edit:(打开编辑页面). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/edit.do")
	public String edit(HttpServletRequest request, HttpServletResponse response){
		List<Sex> sexList = new ArrayList<Sex>();
		Sex sex = new Sex();
		Sex sex1 = new Sex();
		sex.setSex("男");
		sex.setId("0");
		sex1.setSex("女");
		sex1.setId("1");
		sexList.add(sex);
		sexList.add(sex1);
		List<JyEnterprise> JyEnterpriseList= JyEnterpriseBizImpl.getJyEnterpriseList1();
		String id = request.getParameter("id")==null ? "": request.getParameter("id").trim();
		Technician technician = technicianBizImpl.getTechnicianById(id);
		request.setAttribute("technician", technician);
		request.setAttribute("JyEnterpriseList", JyEnterpriseList);
		request.setAttribute("sexList", sexList);
		return "/WEB-INF/manager/mauto/jy/TechnicianXQ";
	}
	
	/**
	 * 
	 * update:(编辑技师信息). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/update.do")
	public String update(HttpServletRequest request,HttpServletResponse response){
		Enumeration<String> enumeration = request.getParameterNames();
		String name = null;
		Map<String,Object> map = new HashMap<String,Object>();
		while(enumeration.hasMoreElements()){
			name = enumeration.nextElement();
			map.put(name,  request.getParameter(name));
		}
		int row1 = technicianBizImpl.getCountByTel(map.get("phone").toString().trim());
		if(row1>0){
			map.put("result", "false");
			map.put("resultMsg", "已存在该用户！");
			JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
			outJson(response, jsonObject);
			return null;
		}
		int row = technicianBizImpl.updateById(map);
		if(row>0){
			map.put("result", "true");
			JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
			outJson(response, jsonObject);
		}
		return null;
	}
	
	/**
	 * 
	 * updatePWD:(重置密码). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = {"updatePWD.do"})
	public String updatePWD(HttpServletRequest request,HttpServletResponse response){
		ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
				SessionConstEnum.MANAGER_SESSION);
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id")==null? "":request.getParameter("id").trim();
		map.put("pwd", StringUtil.Md5("123456"));
		map.put("id", id);
		map.put("updateBy", managerSession.getManagerId());
		int row = technicianBizImpl.updatePWD(map);
		Map<String,Object> map1 = new HashMap<String,Object>();
		if(row>0){
			map.put("result", "true");
		}
		JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
		outJson(response, jsonObject);
		return null;
	}
}
